<?php
/* 文章控制器
* @Author: {daifei}
* @Date:   2014-03-20 21:18:04
* @Last Modified by:   {daifei}
* @Last Modified time: 2014-03-22 18:55:45
*/
class ArticleControl extends CommonControl{
	private $model;
	public function __init(){
		parent::__init();
		$this->model=K('article');
	}
	/**
	 * [index 文章显示页面]
	 * @return [type] [description]
	 */
	public function index (){
		//查看视图模型里面的数据====关联后的表
		$data=K('ArticleView')->join('category')->get_data();
		$this->assign('data' ,$data);
		//接到所有的值  并且将分页排除出去
		$db=$this->model->get_all_art();
		$page=$db['page'];
		unset($db['page']);
		//将分类分配到页面中去
		$this->assign('page' ,$page);
		$this->display();

	}
	/**
	 * [add_art 添加文章]
	 */
	public function add_art(){
		if(IS_POST){
			//判断ajax验证

			if(!$this->model->create()){

				$this->error($this->model->error);

			}

			//判断添加 是否操作成功
			if(!$this->model->add_date()){
				$this->error($this->model->error);
			}
			$this->success('添加成功',U('index'));
		}
		//得到所有的分类  然后分配
		$category=K('category');
		$re=$category->get_data();
		//循环 让pid=0的字段进行一个排序
		$re=Data::channelList($re, 0, '__', 'cid','pid');
		$this->assign('cate' ,$re);
		//分配标签 过去
		$art_tag=K('Tag');
		//得到全部的标签
		$tag=$art_tag->get_data();
		
		unset($tag['page']);
		$this->assign('tag' , $tag);
		$this->display('add.html');

	}
	/**
	 * [_del_art 删除文章 到回收站]
	 * @return [type] [description]
	 */
	public function recycle_art(){
		if(IS_AJAX){
			$aid = Q('aid',0,'intval');
			if($this->model->recycle_data($aid)){
				$return=array(
					'state'		=>1,
					'message'	=>'删除成功,已到回收站',
					'timeout'	=>2
					);
				$this->ajax($return);
			}
		}
	}
	/**
	 * [restore_art 还原文章与上面删除一样 都是该表article表里面的值]
	 * @return [type] [description]
	 */
	public function restore_art(){
		if(IS_AJAX){
			$aid=Q('aid',0,'intval');
			if($this->model->recycle_data($aid,0)){
				$return=array(
					'state'		=>1,
					'message'	=>'还原成功',
					'timeout'	=>2
					);
				$this->ajax($return);
			}
			$this->error('还原错误');
		}


	}
	/**
	 * [recycle_data 回收站里面的删除 直接删除文章]
	 * @return [type] [description]
	 */
	public function del_recycle_data(){
		if(IS_AJAX){
			if($this->model->recycle_del_data()){
				$return=array(
					'state'		=>1,
					'message'	=>'文章永久删除',
					'timeout'	=>2,
					);
				$this->ajax($return);
			}
		}
	}
	
	/**
	 * [recycle 回收站正常显示文章]
	 * @return [type] [description]
	 */
	public function recycle(){
		//回收站显示正常
		$data=K('ArticleView')->join('category')->get_data(null,'1');
		$this->assign('db' , $data);
		$this->display();

	}
	/**
	 * [edit 编辑文章-显示部分]
	 * @return [type] [description]
	 */
	public function edit(){
		$aid=Q('get.aid' , 0 ,'intval');
		$re=K('ArticleView')->where(array('aid'=>$aid))->findAll();
		$this->assign('data' ,current($data));
		$this->display();
	}
}
?>
